﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Interfleet.SE.UICompiler.Tests;
using Interfleet.SE.UICompiler.Core.ClosureCompilerREST;
using Interfleet.SE.UICompiler.Tests.Stubs;

namespace Interfleet.SE.UICompiler.Tests.ClosureCompilerREST
{
    /// <summary>
    /// Summary description for RESTArgumentBuilderTests
    /// </summary>
    [TestClass]
    public class RESTArgumentBuilderTests
    {
        public RESTArgumentBuilderTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void RESTArgumentBuilder_Build_When_InvalidCompileOption_Then_ArgumentExceptionIsThrown()
        {
            var args = new CCRESTArguments();
            args.CompileOption = "test";
            var builder = new CCRESTArgumentBuilder(args);
            var result = builder.Build();
            Assert.Fail();
        }

        [TestMethod]
        public void RESTArgumentBuilder_Build_When_ValidCompileOption_Then_UrlContainsCompilationLevel()
        {
            var args = new CCRESTArguments();
            args.CompileOption = "WHITESPACE_ONLY";
            var builder = new CCRESTArgumentBuilder(args);
            var result = builder.Build();
            StringAssert.Contains(result, "compilation_level=WHITESPACE_ONLY");
        }

        [TestMethod]
        public void RESTArgumentBuilder_Build_When_OutputInfoIsOmitted_Then_UrlContainsCompiledCodeOption()
        {
            var args = new CCRESTArguments();
            args.CompileOption = "WHITESPACE_ONLY";
            var builder = new CCRESTArgumentBuilder(args);
            var result = builder.Build();
            StringAssert.Contains(result, "output_info=compiled_code");
        }

        [TestMethod]
        public void RESTArgumentBuilder_Build_When_OutputInfoIsSet_Then_UrlContainsArgument()
        {
            var args = new CCRESTArguments();
            args.CompileOption = "WHITESPACE_ONLY";
            var builder = new CCRESTArgumentBuilder(args, "errors");
            var result = builder.Build();
            StringAssert.Contains(result, "output_info=errors");
        }

        [TestMethod]
        public void RESTArgumentBuilder_Build_VerifyOutputFormatIsXml()
        {
            var args = new CCRESTArguments();
            args.CompileOption = "WHITESPACE_ONLY";
            var builder = new CCRESTArgumentBuilder(args);
            var result = builder.Build();
            StringAssert.Contains(result, "&output_format=xml");
        }

        [TestMethod]
        public void RESTArgumentBuilder_Build_When_OneFileExists_Then_JSCodeIsContentOfThatFile()
        {
            var args = new CCRESTArguments();
            args.CompileOption = "WHITESPACE_ONLY";
            args.Files.Add("filePath1");
            var builder = new CCRESTArgumentBuilder(args, new FileProviderStub(), new CCRESTCompileOptions());
            var result = builder.Build();
            StringAssert.Contains(result, "js_code=js%20content");
        }

        [TestMethod]
        public void RESTArgumentBuilder_Build_When_TwoFileExists_Then_JSCodeIsContentOfThoseFiles()
        {
            var args = new CCRESTArguments();
            args.CompileOption = "WHITESPACE_ONLY";
            args.Files.Add("filePath1");
            args.Files.Add("filePath2");
            var builder = new CCRESTArgumentBuilder(args, new FileProviderStub(), new CCRESTCompileOptions());
            var result = builder.Build();
            StringAssert.Contains(result, "js_code=js%20contentjs%20content");
        }

        [TestMethod]
        public void RESTArgumentBuilder_Build_VerifyOutputInfoIsCompiledCode()
        {
            var args = new CCRESTArguments();
            args.CompileOption = "WHITESPACE_ONLY";
            var builder = new CCRESTArgumentBuilder(args, new FileProviderStub(), new CCRESTCompileOptions());
            var result = builder.Build();
            StringAssert.Contains(result, "&output_info=compiled_code");
        }


    }
}
